#include "kasm.h"
#include "kc.h"
#include "defs.h"
#include "video.h"
#include "keyboard.h"
#include "defuns.h"
#include "shellfunctions.h"
#include "asmfunc.h"
#include "serial.h"

#define IRQ0	0xFE
#define IRQ1	0xFD
#define IRQ3	0xF7
#define IRQ4	0xEF
#define IRQ8	0xFE

DESCR_INT idt[0x80];			/* IDT de 11 entradas*/
IDTR idtr;				/* IDTR */

int tickpos=640;
unsigned int cursorPos = INIT_CURSOR_POS;
static int timerTick = 0;


void int_08() 
{
	timerTick++;
	
	if((timerTick % CLOCK_GRANULARITY) == 0)
	{
		incrementClock();
		printRCT();
		blinkCursor();

		timerTick = 0;

	}
	
	return;
}


void int_09(int scanCode)
{
	char ascii;
	ascii = toAscii(scanCode);
	if(processSpecial(scanCode) == TRUE)
	{

		if(isKeyPressed(scanCode) && ascii == '\n')
		{	
			processCommand(getKeyBuffer());
			addToKeyBuffer(toAscii(scanCode));
			printPrompt();
		}else if(isKeyPressed(scanCode) && (getEspecials() & S_CAPSLOCK))
			capson();
		else if(isKeyPressed(scanCode) && !(getEspecials() & S_CAPSLOCK))
			capsoff();
			
	
	}else if(isKeyPressed(scanCode) && (getEspecials() & S_CTRL) && (ascii == 'C' || ascii == 'c'))
	{
		cpu();
		printPrompt();
	}else if(isKeyPressed(scanCode) && (getEspecials() & S_CTRL) && (ascii == 'H' || ascii == 'h'))
	{
		help();
		printPrompt();
	}else if(isKeyPressed(scanCode) && (getEspecials() & S_CTRL) && (ascii == 'R' || ascii == 'r'))
	{
		clearBash();
		printPrompt();
	}

	else if(isKeyPressed(scanCode) && ascii!=ND)
	{
		putCharCursor(ascii);
		addToKeyBuffer(ascii);
	}	

	return;
}

void int_IRQ4(unsigned char ch)
{
	static char str[] = "Chat: ";

	if(ch==START_MSJ)
	{
		nextLine();
		writeCursor(str);
	}else if(ch==END_MSJ)
	{
		nextLine();
		printPrompt();
	}else
		putCharCursorAllScreen(ch);
}


/**********************************************
kmain() 
Punto de entrada de codigo C.
*************************************************/

int
kmain() 
{



	setFormat(WHITE_CHAR | BLACK_BACK);
	/* CARGA DE IDT CON LA RUTINA DE ATENCION DE IRQ0    */
	setup_IDT_entry (&idt[0x08], 0x08, (dword)&int_08_hand, ACS_INT, 0);
	/* CARGA DE IDT CON LA RUTINA DE ATENCION DE IRQ1    */
	setup_IDT_entry (&idt[0x09], 0x08, (dword)&int_09_hand, ACS_INT, 0);

	/* CARGA DE IDT CON LA RUTINA DE ATENCION DE IRQ3    */
	setup_IDT_entry (&idt[0x0B], 0x08, (dword)&int_IRQ4_hand, ACS_INT, 0);
	/* CARGA DE IDT CON LA RUTINA DE ATENCION DE IRQ4    */
	setup_IDT_entry (&idt[0x0C], 0x08, (dword)&int_IRQ4_hand, ACS_INT, 0);

	/* CARGA DE IDT CON LA RUTINA DE ATENCION DE IRQ1    */
	//setup_IDT_entry (&idt[0x10], 0x08, (dword)&int_09_hand, ACS_INT, 0);


	
	
/* Carga de IDTR    */

	idtr.base = 0;  
	idtr.base +=(dword) &idt;
	idtr.limit = sizeof(idt)-1;
	
	_lidt (&idtr);	
	_Cli();

/* Habilito interrupcion de timer tick*/

	 /*habilito el TTIC, teclado, com1 y com2*/
	mascaraPIC1(IRQ0 & IRQ1 & IRQ3 & IRQ4);
        mascaraPIC2(0xFF);
	//mascaraPIC1(0x00); /*habilito todo */
        //mascaraPIC2(0x00);


	_Sti();


	saveRTC();
	clearScreen();	
	freeKeyBuffer();
	printPrompt();
	

	setFormat(WHITE_CHAR | BLACK_BACK);
	//updateClock();

	setup_serial(PORT_COM1, 2400, BITS_8 | PARITY_NONE | STOP_ONE);
	//setup_serial(PORT_COM2, 2400, BITS_8 | PARITY_NONE | STOP_ONE);

	setcursor(120,1); //hago desaparecer al cursor

	//setdelay(1,1);
	typematic();
	
        while(1)
        {
        }
}
